package com.mj.controller;

import com.alibaba.fastjson.JSONObject;
import com.mj.entity.Book;
import com.mj.mapper.BookMapper;
import com.mj.mybatis.MyHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class User {
    @RequestMapping("/a")
    public String a() {
        System.out.println("a");
        return "redirect:index.jsp";
    }
    @RequestMapping("/b") @ResponseBody
    public String b(Book book){
        System.out.println(book.getName());
        System.out.println(book.getPrice());
        return null;
    }
    @RequestMapping("/c")
    public String c(Model m){
        MyHelper<BookMapper> mh = new MyHelper<BookMapper>(BookMapper.class);
        BookMapper bm = mh.getMapper();
        m.addAttribute("books",bm.queryAll());
        return "book";
    }
    @RequestMapping("/bookdelete/{id}")
    public String d(@PathVariable("id") int id){
        MyHelper<BookMapper> mh = new MyHelper<BookMapper>(BookMapper.class);
        BookMapper bm = mh.getMapper();
        bm.deleteById(id);
        mh.close();
        return "redirect:/c";
    //return "redirect:/book";
    }
    public @RequestMapping("/d") @ResponseBody String e(){
        String[] ass = new String[]{"毛泽东","列宁","马克思"};
        return JSONObject.toJSONString(ass);
    }
}
